package api

import (
	auto "beergo/app/check/auto/controller"
	"beergo/app/check/conf"
	"beergo/app/check/models"
	"beergo/conf/const/code"
	"beergo/lib/tool"
	"github.com/beego/beego/v2/server/web"
)

type ServiceAdminController struct {
	auto.ServiceController
}

func init() {
	web.Router(conf.UrlPrefix + "/Service/:id", 		&ServiceAdminController{}, "*:GetAction")
	web.Router(conf.UrlPrefix + "/Service/info", 		&ServiceAdminController{}, "*:InfoAction")
	web.Router(conf.UrlPrefix + "/Service/scan", 		&ServiceAdminController{}, "*:ScanAction")
	web.Router(conf.UrlPrefix + "/Service/list", 	    &ServiceAdminController{}, "*:ListAction")
	web.Router(conf.UrlPrefix + "/Service/update", 	&ServiceAdminController{}, "*:UpdateAction")
	web.Router(conf.UrlPrefix + "/Service/create", 	&ServiceAdminController{}, "*:CreateAction")
	web.Router(conf.UrlPrefix + "/Service/copy", 	    &ServiceAdminController{}, "*:CopyAction")
	web.Router(conf.UrlPrefix + "/Service/edit", 	    &ServiceAdminController{}, "*:EditAction")
	web.Router(conf.UrlPrefix + "/Service/delete", 	&ServiceAdminController{}, "*:DeleteAction")
	web.Router(conf.UrlPrefix + "/Service/:id/:field", &ServiceAdminController{}, "*:SonsAction")
	web.Router(conf.UrlPrefix + "/Service/choice", &ServiceAdminController{}, "*:ChoiceAction")
	web.Router(conf.UrlPrefix + "/Service/category", &ServiceAdminController{}, "*:CategoryAction")
}
//func (this *ServiceAdminController) Prepare() {}
func (this *ServiceAdminController) ListAction() {
	admin:=this.GetAdminUser()
	//rep := models.OrgRepository{}
	//org:=rep.FindOneBy(tool.M{"User":admin.Id},nil)
	if admin.Id > 0 {
		this.QueryBuilder.Conditions["org__user__id"] = admin.Id
	}
	lists, err := this.Repository.GetList(this.QueryBuilder)
	if err != nil {
		this.StopError(code.Error, "系统错误", err.Error())
	}
	this.JsonData["lists"] = lists
	this.JsonData["query"] = this.QueryBuilder.FeedBack()
	this.ReturnSuccess("OK", this.JsonData)
}
//func (this *ServiceAdminController) GetAction() {}
//func (this *ServiceAdminController) InfoAction() {}
//func (this *ServiceAdminController) ScanAction() {}
func (this *ServiceAdminController) CreateAction() {
	entity := this.Model.New()
	_, err := this.JsonParseModel(entity)
	this.AnaliseErrors(err)
	admin:=this.GetAdminUser()
	rep := models.OrgRepository{}
	org:=rep.FindOneBy(tool.M{"User":admin.Id},nil)
	if org.Id > 0 {
		entity.Org = & models.Org{Id: org.Id}
	}
	_, errSave := entity.Save()
	this.AnaliseErrors(errSave)
	this.FillM2M(entity)
	this.ReturnSuccess("OK", entity.Id)
}
//func (this *ServiceAdminController) UpdateAction() {}
//func (this *ServiceAdminController) CopyAction() {}
//func (this *ServiceAdminController) DeleteAction() {}
//func (this *ServiceAdminController) EditAction() {}
//func (this *ServiceAdminController) SonsAction() {}
//func (this *ServiceAdminController) ChoiceAction() {}
//func (this *ServiceAdminController) CategoryAction() {}
